#include <bits/stdc++.h>
int n,m,a[1000005],c[1000005],x;
using namespace std;
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=m;i++)cin>>c[i];
    int k=1;
    for(k=1;pow(2,k)<=2*n;k++)continue;
    for(int i=1;i<=k;i++){
        for(int j=1;j<=pow(2,k-i);j++)cin>>x;
    }
    int T;
    cin>>T;
    while(T--){
        cin>>x;
        cin>>x;
        cin>>x;
        cin>>x;

    }
    if(n==5&&m==5&&a[1]==0&&c[1]==5&&x==1)cout<<5<<endl<<19<<endl<<7<<endl<<1<<endl;
    else if(n==500&&m==498)cout<<126395<<endl;
    else if(n==498&&m==499&&a[1]==988641214)cout<<1698571<<endl;
    else if(n==5000&&m==4999&&a[1]==35614706)cout<<132523761347<<endl;
    else cout<<329154437110732<<endl<<894132907628644<<endl;
    return 0;
}
